<?php
namespace Home\Controller;
use Think\Controller;
class ActivityController extends Controller {

	// 创新活动列表
	public function showList() {
		$activity=D('activity');
		$show=$activity->select();
		$arr=array(); 
		foreach ($show as $k => $v) {
				$startdate=$v['startdate'];
				$enddate=$v['enddate'];
				$now=time();
				if ($now>strtotime($enddate)){
					$v['status']='2';
				}
				else if($now<strtotime($enddate) && $now>strtotime($startdate)){
					$v['status']='1';
				}
				else if($now<strtotime($enddate) && $now<strtotime($startdate)){
					$v['status']='0';
				}
			$arr[$k]=$v;
		}
		foreach ($arr as $k => $v) {
			$activity->save($v); 
		}
		$str="0,1";
		$where['status']=array('IN',$str);
		$count=$activity->where($where)->count();
		$page=getpage($count,4);
		$show=$activity->where($where)->limit($page->firstRow,$page->ListRow)->order('adate desc')->select();
		for($i = 0; $i < count($show); $i ++) { // 有多少审核通过的项目就循环多少次
			$adate= $show[$i] ['adate']; // 取出发布时间
			$sub = substr ( $adate, 11, 2 ); // 将发布时间的时截取出来
			if ($sub < 12) {
			$show [$i] ['time'] = "上午"; // 判断是否小于12，如果小于12，就是上午，否则就是下午
			} else {
			$show [$i] ['time'] = "下午";
			}
		}
		$this->assign('list',$show);
		$this->assign('page', $page->show()); 
		$this->display ();
	}

	public function endList() {
		$activity=D('activity');
		$count=$activity->where('status=2')->count();
		$page=getpage($count,4);
		$show=$activity->where('status=2')->limit($page->firstRow,$page->ListRow)->order('adate desc')->select();
		for($i = 0; $i < count($show); $i ++) { // 有多少审核通过的项目就循环多少次
			$adate = $show [$i] ['adate']; // 取出发布时间
			$sub = substr ( $adate, 11, 2 ); // 将发布时间的时截取出来
			if ($sub < 12) {
			$show [$i] ['time'] = "上午"; // 判断是否小于12，如果小于12，就是上午，否则就是下午
			} else {
			$show [$i] ['time'] = "下午";
			}
		}
		$this->assign('list',$show);
		$this->assign('page', $page->show()); 
		$this->display ();
	}
	
	// 我参与的活动列表
	public function myJoin() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 1 ); // 验证学生是否登陆
		$uid=session('userUid');
		$stu_activity=D('stu_activity');
		$activity=$stu_activity->field('a.id,b.*')->alias('a')->join('activity b ON a.aid=b.aid')->where(array('uid'=>$uid))->order('id desc')->select();
		// echo $stu_activity->getLastSql();
		$res=array();
		foreach ($activity as $k=> $v) {
			$now=time();
			$startdate=$v['startdate'];
			$enddate=$v['enddate'];
			if(strtotime($startdate)>$now){
					$v['status']='1';
				}
			if(strtotime($startdate)<$now){
					$v['status']='2';
				}	
			if(strtotime($enddate)<$now){
					$v['status']='3';
				}

				$res[$k]=$v;
		}
		$this->assign('list',$res);
		$this->display ();
	}
	
	// 创新活动详细
	public function details($aid) {
		$activity=D('activity');
		$activity->where(array('aid'=>$aid))->setInc('onclink'); // 用户的积分加1
		$res=$activity->find($aid);
				$startdate=$res['startdate'];
				$enddate=$res['enddate'];
				$bmdate=$res['bmdate'];
				$now=time();
				if($now<strtotime($bmdate)){
					$res['status']='0';
				}
			 	if(strtotime($startdate)>$now && $now>strtotime($bmdate)){
					$res['status']='1';
				}
				if($now>strtotime($startdate)||$res['restticket']=="0"){
					$res['status']='2';
				}
				if($now>strtotime($enddate)){
					$res['status']='3';
				}
		 $res['startdate']=date('Y-m-d H:m',strtotime($startdate));
		 $res['enddate']=date('Y-m-d H:m',strtotime($enddate));
		 $res['bmdate']=date('Y-m-d H:m',strtotime($bmdate));
		$this->assign('list',$res);
		$this->display ();
	}

	public function invest($uid,$aid) {
		$data['uid']=$uid;
		$data['aid']=$aid;
		$stu=D('stu_activity');
		$isP=$stu->where(array('uid'=>$uid,'aid'=>$aid))->find();
		if (!$isP) {
			$res=$stu->add($data);
			if ($res) {
			$activity=D('activity');
			$activity->where(array('aid'=>$aid))->setDec('restticket'); // 用户的积分加1
			$restticket=$activity->field('restticket')->find($aid);
				  echo $restticket['restticket'];
			}else{
				echo "0";
			}
		}
		else{
			echo "false";
		}
	}

	public function cancel($uid,$aid) {
		$data['uid']=$uid;
		$data['aid']=$aid;
		$stu=D('stu_activity');
		$res=$stu->where(array('uid'=>$uid,'aid'=>$aid))->delete();
		$activity=D('activity');
		$activity->where(array('aid'=>$aid))->setInc('restticket');
		$restticket=$activity->field('restticket')->find($aid);
		if ($res) {
			   echo $restticket['restticket'];
			}else{
				echo "0";
			}
		}

		public function isjoin($uid,$aid){
			$stu=D('stu_activity');
			$isP=$stu->where(array('uid'=>$uid,'aid'=>$aid))->find();
			if($isP){
				echo "false";
			}
			else{
			echo "true";
			}
		}

  public function download($aid){
  		$aid=$_GET['aid'];
		$res=D('activity')->find($aid);
		$url='D:/xampp/htdocs'.__ROOT__.'/Public/UPLOADS/activity/'.$res['thumb'];
		$this->download_file($url,$res['attach']);
  }
 function download_file($file,$showname){
    if(is_file($file)){
        $length = filesize($file);
        $type = mime_content_type($file);
        // $showname =  ltrim(strrchr($file,'/'),'/');
        header("Content-Description: File Transfer");
        header('Content-type: ' . $type);
        header('Content-Length:' . $length);
         if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
             header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
         } else {
             header('Content-Disposition: attachment; filename="' . $showname . '"');
         }
         ob_clean(); 
         flush(); 
         readfile($file);
         exit;
     } else {
         exit('文件已被删除！');
     }
 }

public function set_session($aid){
       session('activityLogin',$aid);
}

 

}

